//
//  CZUserAccountModel.swift
//  微博
//
//  Created by 霸气的刘吉庆 on 16/4/19.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit


//需要进行数据的存储,所以需要使用归档和反归档
class CZUserAccountModel: NSObject,NSCoding {
    
    //定义属性
    
    /*
    
    "access_token": "ACCESS_TOKEN",
    "expires_in": 1234,
    "remind_in":"798114",
    "uid":"12341234"
    
    */
    
    
    var access_token: String?
    
    
    //判断生命周期是否有效
    var expires_in: NSTimeInterval = 0 {
        didSet{
            // 计算过期时间
            
//            Double
            expireTime = NSDate(timeIntervalSinceNow: expires_in)
            
            print(expireTime)
        }
    }
    
    var expireTime: NSDate?
    
    var remind_in: String?
    
    var uid: String?
    
    //还需要什么元素,图片的url
    
    var avatar_large: String?
    
    var name: String?
    
    
   
    
    //用户头像地址,(大图) 180*180
    
    
    
    
    //还有什么东西,以后再添加
    
    //这个还要进行归档和解档,归档和解档不是在这里
    
    init(dict: [String: AnyObject]) {
        super.init()

        
        setValuesForKeysWithDictionary(dict)
        
        
        print(dict)
        
    }
    
    required init?(coder decoder: NSCoder) {
        
        
        //这个是接档
        
        uid = decoder.decodeObjectForKey("uid") as? String
        
        access_token = decoder.decodeObjectForKey("access_token") as? String
        
        
        remind_in = decoder.decodeObjectForKey("remind_in") as? String
        
        expires_in = decoder.decodeDoubleForKey("expires_in")
        
        

        
        name = decoder.decodeObjectForKey("name") as? String
  
        avatar_large = decoder.decodeObjectForKey("avatar_large") as? String

        
        expireTime = decoder.decodeObjectForKey("expireTime") as? NSDate


        
        
    }
    
    //归档
    
    func encodeWithCoder(encoder: NSCoder) {
        
        //归档将内容进行操作
        
//        encoder.encodeObject(<#T##object: AnyObject?##AnyObject?#>)
        
        //归档,将内容以规定的方式存储
        encoder.encodeObject(uid, forKey: "uid")
        
//        encoder.encodeInteger(expires_in, forKey: "expires_in")
        
        
        
        encoder.encodeObject(access_token, forKey: "access_token")
        
        encoder.encodeObject(remind_in, forKey: "remind_in")
        
        encoder.encodeDouble(expires_in, forKey: "expires_in")
        
        encoder.encodeObject(name, forKey: "name")
        
        encoder.encodeObject(avatar_large, forKey: "avatar_large")
        
        
        //过期时间也需要归档
        
        encoder.encodeObject(expireTime, forKey: "expireTime")
        
        
        
        
        
        
    }
    
    
    
    
    
    
    
    
    
    

}
